package com.penghaisoft.microwms.product.web.controller.intowarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.wms.model.ProCheckIn;
import com.penghaisoft.microwms.product.service.IProBarcodeService;
import com.penghaisoft.microwms.product.service.IProCheckInService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping(value = "/checkin")
@Slf4j
public class ProCheckInController extends BaseController {

    @Autowired
    private IProCheckInService proCheckInService;

    @Autowired
    private IWmsCommonService wmsCommonService;

    @Autowired
    private IProBarcodeService proBarcodeService;

    public TransResult create(@RequestBody List<ProCheckIn> proCheckIns) {
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
    	ProCheckIn proCheckIn = new ProCheckIn();
        proCheckIn = proCheckIns.get(0);
        proCheckIn.preInsert();
        proCheckIn.setActiveFlag("1");
        String checkInNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_TC, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
        proCheckIn.setCheckInNo(checkInNo);
        proCheckIn.setCreateBy(getLoginName());
        Resp resp = proCheckInService.create(proCheckIn);
//        return fromResp(resp);
        result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
    }
    /**
     * 新增记录
     *wzhg
     * @param proCheckIns
     * @return
     */
    @PostMapping("c")
    public TransResult create1(@RequestBody List<ProCheckIn> proCheckIns) {
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
    	Resp resp = new Resp();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ProCheckIn proCheckIn = new ProCheckIn();
        int count = proCheckIns.size();
        List<String> checkInNos = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_DJ, 
        		Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1);
        for(int i = 0;i<count;i++){
            proCheckIn = proCheckIns.get(i);
            try {
                proCheckIn.setGmtProduct(sdf.parse(sdf.format(proCheckIn.getGmtProduct())));
            }catch (Exception e){

            }
            proCheckIn.preInsert();
            proCheckIn.setActiveFlag("1");
            String checkInNo = checkInNos.get(0);
            proCheckIn.setCheckInNo(checkInNo);
            proCheckIn.setCreateBy(getLoginName());
            proCheckIn.setLastModifiedBy(getLoginName());
            resp = proCheckInService.create(proCheckIn);
            result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
        }
        return result;
    }
    
    /**
     * 重卷报废
     */
    @PostMapping("packScrap")
    public TransResult packScrap(@RequestBody List<ProCheckIn> proCheckIns) {
    	TransResult result = new TransResult();
        if(proCheckIns.size() == 1){
	        List<String> checkInNos = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_BF, 
	        		Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1);
	        ProCheckIn proCheckIn = proCheckIns.get(0);
	        proCheckIn.setCheckInNo(checkInNos.get(0));
	        proCheckIn.setLoginName(getLoginName());
	        Resp resp = proBarcodeService.packScrap(proCheckIn);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("数据错误，请联系运维人员！");
        }
        return result;
    }

    /**
     * 拆分报废
     */
    @PostMapping("splitScrap")
    public TransResult splitScrap(@RequestBody List<ProCheckIn> proCheckIns) {
        TransResult result = new TransResult();
        int j = 0;
        if(proCheckIns.size() >= 1){
            List<String> checkInNos = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_BF,
                    Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1);
            int count = 0;
            for(int i=0;i<proCheckIns.size();i++){
                ProCheckIn proCheckIn = proCheckIns.get(i);
                proCheckIn.setCheckInNo(checkInNos.get(0));
                proCheckIn.setLoginName(getLoginName());
                Resp resp = proBarcodeService.splitScrap(proCheckIn);
                j = j + Integer.valueOf(resp.getCode());
            }
            result.setCode(Constant.SUCCESS);
            result.setMsg("条码生成完毕");
        }else{
            result.setCode(Constant.FAIL);
            result.setMsg("数据错误，请联系运维人员！");
        }
        return result;
    }

    @PostMapping("codeCreate")
    public TransResult codeCreate(ProCheckIn proCheckIn){
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
//        String checkInNo = null;
//        checkInNo = proCheckIn.getCheckInNo();
        String  checkInId = null;
        checkInId = proCheckIn.getCheckInId().toString();
        ProCheckIn checkIn = proCheckInService.findById(checkInId);
        checkIn.setLoginName(getLoginName());
        Resp resp = proBarcodeService.codeCreate(checkIn);
        if(Constant.FAIL.equals(resp.getCode())){
        	result.setCode(resp.getCode());
        	result.setMsg(resp.getMsg());
        	return result;
        }
//        return fromResp(resp);
        return result;
    }

    /**
     * 删除记录（逻辑删除active_flag=0)
     *
     * @param proCheckIn
     * @return
     */
    @PostMapping("d")
    public TransResult delete(ProCheckIn proCheckIn) {
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
        proCheckIn.preUpdate();
        Resp resp = proCheckInService.delete(proCheckIn);
//        return fromResp(resp);
        result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
    }

    /**
     * 查询列表
     *
     * @param
     * @return
     */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProCheckIn proCheckIn) {
        Pager<ProCheckIn> result = proCheckInService.findListByCondition(page, rows, proCheckIn);
        return success(result);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
        ProCheckIn proCheckIn = proCheckInService.findById(id);
        return success(proCheckIn);
    }

    /**
     * 修改记录
     *
     * @param proCheckIn
     * @return
     */
    @PostMapping("u")
    public TransResult update(ProCheckIn proCheckIn) {
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
        proCheckIn.preUpdate();
        Resp resp = proCheckInService.update(proCheckIn);
//        return fromResp(resp);
        result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
    }

}
